/* eslint-disable @typescript-eslint/no-unused-vars */
/**
 * @returns  十一月-初一
 */
export function getLunarBy(year: number, month: number, day: number) {
  const lun = Lunar(year, month - 1, day)
  let lmonth = getLunMonth(lun.month)
  const lday = getLunDay(lun.day)
  const lleap = lun.isLeap
  if (lleap == 1) {
    lmonth = '闰' + lmonth
  }
  return `${lmonth}月-${lday}`
}
/**
 * 获取农历
 * @param newDate  新历yyyy-MM-dd
 * @returns 正月十五
 */
export function getLunar(newDateStr: string) {
  const ss = newDateStr.split('-')
  const lun = Lunar(parseInt(ss[0]), parseInt(ss[1]) - 1, parseInt(ss[2]))
  let lmonth = getLunMonth(lun.month)
  const lday = getLunDay(lun.day)
  const lleap = lun.isLeap
  if (lleap == 1) {
    lmonth = '闰' + lmonth
  }
  return `${lmonth}月${lday}`
}

/**
 * 获取农历
 * @param newDateStr  新历yyyy-MM-dd
 * @returns  2023年正月十五
 */
export function getLunarFull(newDateStr: string, containerWeek?: boolean) {
  const ss = newDateStr.split('-')
  const lun = Lunar(parseInt(ss[0]), parseInt(ss[1]) - 1, parseInt(ss[2]))
  const lyear = lun.year
  let lmonth = getLunMonth(lun.month)
  const lday = getLunDay(lun.day)
  const lleap = lun.isLeap
  if (lleap == 1) {
    lmonth = '闰' + lmonth
  }
  if (containerWeek) {
    return lyear + '年' + lmonth + '月' + lday
  } else {
    return lyear + '年' + lmonth + '月' + lday
  }
}
//辅助函数
const hzWeek = ['日', '一', '二', '三', '四', '五', '六', '日']

function cweekday(wday: number) {
  return hzWeek[wday]
}
//newDay,新历日期2023-01-01
function getCalendarObj(newDay: string) {
  const d = new Date(newDay)
  //
  const month = d.getMonth() + 1
  const day = d.getDate()
  const week = d.getDay()
  const lunMonth = getLunMonth(month)
  const lunDay = getLunDay(day)
  const calendarObj = {
    source: newDay,
    year: d.getFullYear(),
    month: month,
    day: day,
    week: week,
    weekName: hzWeek[week],
    lunMonth: lunMonth,
    lunDay: lunDay,
    lun: lunMonth + lunDay,
  }
  console.log(calendarObj)
  return calendarObj
}

//输入2020-0101或20200101，输出农历：正月初一
function getLunarByLun(lunDate: string) {
  if (lunDate == null || lunDate.length <= 0) {
    return ''
  }
  if (lunDate.length == 4) {
    return ''
  }
  lunDate = lunDate.replace('闰', '')
  if (lunDate.length == 8) {
    //19990101
    const y = lunDate.substring(0, 4)
    const m = lunDate.substring(4, 6)
    const d = lunDate.substring(6)
    lunDate = y + '-' + m + '-' + d
  }
  //1999-01-01
  if (lunDate.length == 10) {
    const ss = lunDate.split('-')
    const month = parseInt(ss[1])
    const day = parseInt(ss[2])
    return getLunMonth(month) + getLunDay(day)
  } else {
    return ''
  }
}

function getLunAnimal(year: number) {
  switch (year % 12) {
    case 0:
      return '猴'
    case 1:
      return '鸡'
    case 2:
      return '狗'
    case 3:
      return '猪'
    case 4:
      return '鼠'
    case 5:
      return '牛'
    case 6:
      return '虎'
    case 7:
      return '兔'
    case 8:
      return '龙'
    case 9:
      return '蛇'
    case 10:
      return '马'
    default:
      return '羊'
  }
}

function getMonthDays(year: number, month: number) {
  let count = 0
  //判断大月份
  if (
    month == 1 ||
    month == 3 ||
    month == 5 ||
    month == 7 ||
    month == 8 ||
    month == 10 ||
    month == 12
  ) {
    count = 31
  }
  //判断小月
  if (month == 4 || month == 6 || month == 9 || month == 11) {
    count = 30
  }
  //判断平年与闰年
  if (month == 2) {
    if (isLeapYear(year)) {
      count = 29
    } else {
      count = 28
    }
  }
  return count
}
/**
 * 是否是闰年
 *
 * @param year year
 * @return 是否是闰年
 */
function isLeapYear(year: number) {
  return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
}

function lYearDays(y: number) {
  let i,
    sum = 348
  for (i = 0x8000; i > 0x8; i >>= 1) sum += lunarInfo[y - 1900] & i ? 1 : 0
  return sum + leapDays(y)
}

function leapDays(y: number) {
  if (leapMonth(y)) return lunarInfo[y - 1900] & 0x10000 ? 30 : 29
  else return 0
}

function leapMonth(y: number) {
  return lunarInfo[y - 1900] & 0xf
}

function monthDays(y: number, m: number) {
  return lunarInfo[y - 1900] & (0x10000 >> m) ? 30 : 29
}
interface LunarInitBean {
  year: number
  isLeap: number
  month: number
  day: number
}
function Lunar(y: number, m: number, d: number): LunarInitBean {
  let i,
    leap = 0
  let temp = 0
  let offset =
    (Date.UTC(parseInt(y.toString()), parseInt(m.toString()), parseInt(d.toString())) -
      Date.UTC(1900, 0, 31)) /
    86400000
  for (i = 1900; i < 2050 && offset > 0; i++) {
    temp = lYearDays(i)
    offset -= temp
  }
  if (offset < 0) {
    offset += temp
    i--
  }
  const year = i
  leap = leapMonth(i)
  let isLeap: boolean = false
  for (i = 1; i < 13 && offset > 0; i++) {
    if (leap > 0 && i == leap + 1 && isLeap == false) {
      --i
      isLeap = true
      temp = leapDays(year)
    } else {
      temp = monthDays(year, i)
    }
    if (isLeap == true && i == leap + 1) isLeap = false
    offset -= temp
  }
  if (offset == 0 && leap > 0 && i == leap + 1)
    if (isLeap) {
      isLeap = false
    } else {
      isLeap = true
      --i
    }
  if (offset < 0) {
    offset += temp
    --i
  }
  const month = i
  const day = offset + 1
  return { year: year, month: month, day: day, isLeap: isLeap ? 1 : 0 }
}
const nStr1 = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二']
const nStr2 = ['初', '十', '廿', '卅', '□']
/**
 * @returns 正月
 */
function getLunMonth(month: number) {
  if (month == 1) return '正'
  else return nStr1[month]
}
/**
 * @returns  初一
 */
function getLunDay(d: number) {
  let s
  switch (d) {
    case 10:
      s = '初十'
      break
    case 20:
      s = '二十'
      break
    case 30:
      s = '三十'
      break
    default:
      s = nStr2[Math.floor(d / 10)]
      s += nStr1[d % 10]
      break
  }
  return s
}

//农历函数开始
const lunarInfo = [
  0x04bd8,
  0x04ae0,
  0x0a570,
  0x054d5,
  0x0d260,
  0x0d950,
  0x16554,
  0x056a0,
  0x09ad0,
  0x055d2,
  0x04ae0,
  0x0a5b6,
  0x0a4d0,
  0x0d250,
  0x1d255,
  0x0b540,
  0x0d6a0,
  0x0ada2,
  0x095b0,
  0x14977,
  0x04970,
  0x0a4b0,
  0x0b4b5,
  0x06a50,
  0x06d40,
  0x1ab54,
  0x02b60,
  0x09570,
  0x052f2,
  0x04970,
  0x06566,
  0x0d4a0,
  0x0ea50,
  0x06e95,
  0x05ad0,
  0x02b60,
  0x186e3,
  0x092e0,
  0x1c8d7,
  0x0c950,
  0x0d4a0,
  0x1d8a6,
  0x0b550,
  0x056a0,
  0x1a5b4,
  0x025d0,
  0x092d0,
  0x0d2b2,
  0x0a950,
  0x0b557,
  0x06ca0,
  0x0b550,
  0x15355,
  0x04da0,
  0x0a5b0,
  0x14573,
  0x052b0,
  0x0a9a8,
  0x0e950,
  0x06aa0,
  0x0aea6,
  0x0ab50,
  0x04b60,
  0x0aae4,
  0x0a570,
  0x05260,
  0x0f263,
  0x0d950,
  0x05b57,
  0x056a0,
  0x096d0,
  0x04dd5,
  0x04ad0,
  0x0a4d0,
  0x0d4d4,
  0x0d250,
  0x0d558,
  0x0b540,
  0x0b6a0,
  0x195a6,
  0x095b0,
  0x049b0,
  0x0a974,
  0x0a4b0,
  0x0b27a,
  0x06a50,
  0x06d40,
  0x0af46,
  0x0ab60,
  0x09570,
  0x04af5,
  0x04970,
  0x064b0,
  0x074a3,
  0x0ea50,
  0x06b58,
  0x05ac0,
  0x0ab60,
  0x096d5,
  0x092e0, //1990
  0x0c960,
  0x0d954,
  0x0d4a0,
  0x0da50,
  0x07552,
  0x056a0,
  0x0abb7,
  0x025d0,
  0x092d0,
  0x0cab5,
  0x0a950,
  0x0b4a0,
  0x0baa4,
  0x0ad50,
  0x055d9,
  0x04ba0,
  0x0a5b0,
  0x15176,
  0x052b0,
  0x0a930,
  0x07954,
  0x06aa0,
  0x0ad50,
  0x05b52,
  0x04b60,
  0x0a6e6,
  0x0a4e0,
  0x0d260,
  0x0ea65,
  0x0d530,
  0x05aa0,
  0x076a3,
  0x096d0,
  0x04bd7,
  0x04ad0,
  0x0a4d0,
  0x1d0b6,
  0x0d250,
  0x0d520,
  0x0dd45,
  0x0b5a0,
  0x056d0,
  0x055b2,
  0x049b0,
  0x0a577,
  0x0a4b0,
  0x0aa50,
  0x1b255,
  0x06d20,
  0x0ada0,
  0x14b63,
]
